﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SharpSoft.WPF.Controls
{
    /// <summary>
    /// IconLabel.xaml 的交互逻辑
    /// </summary>
    public partial class IconLabel : ContentControl
    {
        static IconLabel()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(IconLabel), new FrameworkPropertyMetadata(typeof(IconLabel)));
        }
        public IconLabel()
        {
            this.VerticalContentAlignment = VerticalAlignment.Center;
            this.HorizontalContentAlignment = HorizontalAlignment.Center;
        }
        /// <summary>
        /// 图标
        /// </summary>
        public ImageSource Icon
        {
            get { return (ImageSource)GetValue(IconProperty); }
            set { SetValue(IconProperty, value); }
        }
        public static readonly DependencyProperty IconProperty =
            DependencyProperty.Register("Icon", typeof(ImageSource), typeof(IconLabel), new PropertyMetadata(default(ImageSource)));
        /// <summary>
        /// 图标的水平布局方式
        /// </summary>
        public HorizontalAlignment HorizontalIconAlignment
        {
            get { return (HorizontalAlignment)GetValue(HorizontalIconAlignmentProperty); }
            set { SetValue(HorizontalIconAlignmentProperty, value); }
        }

        public static readonly DependencyProperty HorizontalIconAlignmentProperty =
            DependencyProperty.Register("HorizontalIconAlignment", typeof(HorizontalAlignment), typeof(IconLabel), new PropertyMetadata(HorizontalAlignment.Center));
        /// <summary>
        /// 图标的垂直布局方式
        /// </summary>
        public VerticalAlignment VerticalIconAlignment
        {
            get { return (VerticalAlignment)GetValue(VerticalIconAlignmentProperty); }
            set { SetValue(VerticalIconAlignmentProperty, value); }
        }

        public static readonly DependencyProperty VerticalIconAlignmentProperty =
            DependencyProperty.Register("VerticalIconAlignment", typeof(VerticalAlignment), typeof(IconLabel), new PropertyMetadata(VerticalAlignment.Center));



        /// <summary>
        /// 图标尺寸
        /// </summary>
        public Size? IconSize
        {
            get { return (Size?)GetValue(IconSizeProperty); }
            set { SetValue(IconSizeProperty, value); }
        }

        public static readonly DependencyProperty IconSizeProperty =
            DependencyProperty.Register("IconSize", typeof(Size?), typeof(IconLabel), new PropertyMetadata(null));

        /// <summary>
        /// 图标拉伸方式
        /// </summary>
        public Stretch IconStretch
        {
            get { return (Stretch)GetValue(IconStretchProperty); }
            set { SetValue(IconStretchProperty, value); }
        }

        public static readonly DependencyProperty IconStretchProperty =
            DependencyProperty.Register("IconStretch", typeof(Stretch), typeof(IconLabel), new PropertyMetadata(Stretch.Fill));
        /// <summary>
        /// 图标的外边距
        /// </summary>
        public Thickness IconMargin
        {
            get { return (Thickness)GetValue(IconMarginProperty); }
            set { SetValue(IconMarginProperty, value); }
        }

        public static readonly DependencyProperty IconMarginProperty =
            DependencyProperty.Register("IconMargin", typeof(Thickness), typeof(IconLabel), new PropertyMetadata(new Thickness()));



        /// <summary>
        /// 图标停靠方式
        /// </summary>
        public Dock IconDock
        {
            get { return (Dock)GetValue(IconDockProperty); }
            set { SetValue(IconDockProperty, value); }
        }

        public static readonly DependencyProperty IconDockProperty =
            DependencyProperty.Register("IconDock", typeof(Dock), typeof(IconLabel), new PropertyMetadata(Dock.Left));

        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
            if (e.Property == IconSizeProperty)
            {
                applyIconSize();
            }

        }
        Image icon = null;
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            icon = this.GetTemplateChild("ICON") as Image;
            applyIconSize();
        }

        private void applyIconSize()
        {
            if (icon != null)
            {
                if (this.IconSize.HasValue)
                {
                    icon.Width = this.IconSize.Value.Width;
                    icon.Height = this.IconSize.Value.Height;
                }
                else
                {
                    icon.Width = double.NaN;
                    icon.Height = double.NaN;
                }
            }
        }
    }
}
